package com.sgb.kjwl.viewmodel.pick;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.pick.DemandSearchBean;
import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.ProjectListEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.sgb.kjwl.repository.PickRepertory;
import com.sgb.kjwl.view.ui.fragment.pick.DemandSearchListFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

public class DemandlistModle extends BaseViewModel {
    DemandSearchListFragment fragment;
    PickRepertory repertory;
    private boolean isLastPage;
    private String longitude;
    private String latitude;

    public DemandlistModle(DemandSearchListFragment fragment, PickRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }
    /**
     * 地址详情
     */
    public void getAddressModel() {
    DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
    APIManager.getInstance().getPickAPI().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
        @Override
        public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
            DialogHelper.dismissProgressDialog();
            if (data != null && data.getData() != null && data.getData().size() > 0) {
                fragment.setAddressList(data.getData());
            }
        }
    });
    }

    //排序
    public void getSortType() {
        List<CommonPopEntity> datas = new ArrayList<>();
        datas.add(new CommonPopEntity("1", "综合排序"));
        datas.add(new CommonPopEntity("3", "创建时间↓"));
        datas.add(new CommonPopEntity("2", "创建时间↑"));
        datas.add(new CommonPopEntity("4", "距离↓"));
        datas.add(new CommonPopEntity("6", "项目造价↓"));
        datas.add(new CommonPopEntity("5", "项目造价↑"));
        fragment.setSortTypeList(datas);
    }


    //工程类型
    public void getProjectTypeList() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getFindEngineeringTypeSelect().enqueue(new com.swgk.core.base.api.ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    fragment.setProjectTypeList(data.getData());
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }
    //创建时间
    public void getTime() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getfindProjectTimeSelect().enqueue(new com.swgk.core.base.api.ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    fragment.setTime(data.getData());
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }

    public void ProjectList(DemandSearchBean requestBean) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getFindNoticeESPage(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<ProjectListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<ProjectListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    if (data.getData() != null){
                        isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                        fragment.setProjectList(data.getData().getList(),isLastPage);
                    }
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }

    public void getH5Data(String id) {
        longitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID,"");
        latitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID,"");
        if (TextUtils.isEmpty(longitude)){
            longitude = "0";
        }
        if (TextUtils.isEmpty(latitude)){
            latitude = "0";
        }
        APIManager.getInstance().getPickAPI().getUrlSetting("engineeringProject").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    String webUrl = data.getData().getUrl() + "?id=" + id + "&lng=" + longitude + "&lat=" + latitude;
                    fragment.showUrl(webUrl);
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }
}
